# -*- coding: utf-8 -*-            
# @Author : HJH
# @Time : 2024/3/12 14:52
# @Use : 往返航线数据匹配
import hashlib
import itertools
import json
from datetime import datetime

import pandas as pd

if __name__ == '__main__':
    file_name = input("输入需要整理文件名(.xlsx格式): ")
    out_file_name = input("输入输出文件名: ")
    print("读取文件数据... ...")
    pd_data = pd.read_excel(f'./{file_name}.xlsx')
    # 将每一行转为字典，并存储在一个列表中
    data_dict_list = pd_data.to_dict(orient='records')

    res_data = []
    for item in data_dict_list:
        cabins = item['cabin'].split(' ')
        cabins = [c for c in cabins if c != '' and c[0] in 'O']

        o_num = cabins[0][1]
        if o_num != 'Q' and o_num != 'S' and o_num != 'L':
            item['cabin'] = 'O' + o_num
            res_data.append(item)

        # cabins = [c for c in cabins if c != '' and c[0] in 'TANR']
        # cabins.reverse()
        # for cabin in cabins:
        #     c = cabin[1]
        #     if c == 'Q' or c == 'S' or c == 'L':
        #         continue
        #     else:
        #         item['cabin'] = cabin
        #         item['cabin-o'] = 'O' + o_num
        #         res_data.append(item)
        #         break

    # 将列表转换为 DataFrame
    df = pd.DataFrame(res_data)

    # # 设置表头
    # header = ['航班号', '出发日期', '']

    print("开始写入文件... ...")
    # 将数据写入 Excel 文件并设置表头
    with pd.ExcelWriter(f'./{out_file_name}.xlsx', engine='xlsxwriter') as writer:
        df.to_excel(writer, index=False, )

    # 输出成功提示
    print("Excel 文件写入成功！")
